﻿using System;
using System.Collections.Generic;
using System.Linq;
using _5E.Infrastructure.Querying;
using WholesaleOnline.Contract;
using WholesaleOnline.DataContract.Messaging;
using WholesaleOnline.Infrastructure.Configuration;
using WholesaleOnline.Model;
using WholesaleOnline.Model.Repository;
using WholesaleOnline.Repository;

namespace WholesaleOnline.Service.Services
{
    public class InventoryService : IInventoryService
    {
        private IAttributeRepository m_attributeRepository = null;
        private IKeywordRepository m_keywordRepository = null;

        public InventoryService(IAttributeRepository attributeRepository, IKeywordRepository keywordRepository)
        {
            m_attributeRepository = attributeRepository;
            m_keywordRepository = keywordRepository;
        }

        public GetInventoryProductsAndCategoriesResponse GetProductsAndCategoriesBy(GetInventoryProductsAndCategoriesRequest request)
        {
            GetInventoryProductsAndCategoriesResponse response = new GetInventoryProductsAndCategoriesResponse();
            try
            {
                IBuyerProductRepository buyerProductRepository = BuyerProductRepositoryFactory.CreateBuyerProductRepository(request.Type);
                response.Products = buyerProductRepository.FindProductsFor(
                    request.BuyerId, request.SellerId, request.PageIndex, request.PageSize, keyword: request.Keyword);

                response.DisplayCategoryIds = buyerProductRepository.FindCategoryIdsFor(
                    request.BuyerId, request.SellerId, request.Keyword).ToDictionary(item => (Guid)item[0], item => true);

                response.Categories = GetCategoriesBy(request.SellerId);
                response.Success = true;
            }
            catch (Exception)
            {
            }
            return response;
        }

        private IEnumerable<WoAttribute> GetCategoriesBy(Guid sellerId)
        {
            Query query = new Query();
            query.QueryOperator = QueryOperator.And;
            query.Add(Criterion.Create<WoAttribute>(attr => attr.ParentId, CriteriaOperator.IsNull));
            query.Add(Criterion.Create<WoAttribute>(attr => attr.Type.Name, CriteriaOperator.Eq,
                MyApplicationSettingsFactory.ApplicationSettings.CategoryAttributeTypeName));
            return m_attributeRepository.FindBy(query);
        }

        public GetInventoryProductsResponse GetProductsBy(GetInventoryProductsRequest request)
        {
            GetInventoryProductsResponse resposne = new GetInventoryProductsResponse();
            try
            {
                IBuyerProductRepository buyerProductRepository = BuyerProductRepositoryFactory.CreateBuyerProductRepository(request.Type);
                resposne.Products = buyerProductRepository.FindProductsFor(
                    request.BuyerId, request.SellerId, request.PageIndex, request.PageSize, request.CategoryId, request.Keyword);
                resposne.Success = true;
            }
            catch (Exception)
            {
            }
            return resposne;
        }

        public GetKeywordsResponse GetKeywords(GetKeywordsRequest request)
        {
            GetKeywordsResponse response = new GetKeywordsResponse();
            try
            {
                Query query = new Query();
                query.QueryOperator = QueryOperator.Or;
                query.Add(Criterion.Create<WoKeyword>(k => k.Abbreviation, CriteriaOperator.LikeAnywhereIgnoreCase, request.Keyword));
                query.Add(Criterion.Create<WoKeyword>(k => k.Value, CriteriaOperator.LikeAnywhereIgnoreCase, request.Keyword));
                query.Add(Criterion.Create<WoKeyword>(k => k.Pinyin, CriteriaOperator.LikeAnywhereIgnoreCase, request.Keyword));
                response.Keywords = m_keywordRepository.FindBy(query, 1, request.Count);
                response.Success = true;
            }
            catch (Exception)
            {
            }
            return response;
        }
    }
}
